package com.uvnos4j.matilda.security.domain;

import cn.hutool.core.date.DateUtil;
import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.Date;

/**
 * 认证用户
 * <p>
 * Description:认证用户
 * </p>
 *
 * @author Guo.wl
 * @version v1.0.0
 * @since 2020-08-17 00:09:35
 */
@Data
public class AuthUser implements UserDetails {
    /**
     * 用户标识
     */
    private Long userId;
    /**
     * 用户名
     */
    private String username;
    /**
     * 密码
     */
    private String password;
    /**
     * 用户帐号状态：0->禁用；1->启用；2->锁定
     */
    private Integer status;
    /**
     * 账号创建时间
     */
    private Date createTime;
    /**
     * 密码到期时间
     */
    private Date editPassTime;


    private Collection<? extends GrantedAuthority> authorities;

    @Override
    public String getUsername() {
        return username;
    }

    @Override
    public String getPassword() {
        return password;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return authorities;
    }

    @Override
    public boolean isAccountNonExpired() {
        // createTime
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return status != 2;
    }

    @Override
    public boolean isEnabled() {
        return status == 1;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return DateUtil.compare(editPassTime, DateUtil.date()) > 0;
    }

    @Override
    public String toString() {
        return "AuthUser { user=" + username + " }";
    }

}